package io.itit.ecp.admin.provide.helper;

import com.baomidou.mybatisplus.core.metadata.IPage;
import io.itit.ecp.admin.provide.action.resp.ISceneInfoResp;
import io.itit.ecp.admin.server.entity.SceneEdificeEntity;
import io.itit.ecp.admin.server.entity.SceneFloorEntity;
import io.itit.ecp.admin.server.entity.SceneRoomEntity;
import io.itit.ecp.admin.server.service.ISceneEdificeService;
import io.itit.ecp.admin.server.service.ISceneFloorService;
import io.itit.ecp.admin.server.service.ISceneRoomService;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.*;
import java.util.stream.Collectors;

/*
 **********************
 * @Author Roy
 * @ClassName SceneHelper
 * @Description 场景辅助类
 * @Date 2023/12/27 10:34
 **********************
 **/
@Component
public class SceneHelper implements InitializingBean {

    @Resource
    private ISceneEdificeService sceneEdificeService;

    @Resource
    private ISceneFloorService sceneFloorService;

    @Resource
    private ISceneRoomService sceneRoomService;

    private static SceneHelper sceneHelper;

    public static SceneHelper i() {

        return sceneHelper;
    }

    @Override
    public void afterPropertiesSet() throws Exception {
        sceneHelper = this;
    }

    /**
     * 设置场景信息
     *
     * @param page
     * @param <T>
     * @return
     */
    public <T extends ISceneInfoResp> IPage<T> setSceneInfo(IPage<T> page) {
        setSceneInfo(page.getRecords());
        return page;
    }

    /**
     * 设置场景信息
     *
     * @param respList
     * @param <T>
     * @return
     */
    public <T extends ISceneInfoResp> List<T> setSceneInfo(List<T> respList) {
        if (CollectionUtils.isEmpty(respList)) {
            return respList;
        }
        Set<String> edificeSet = respList.stream().filter(t -> StringUtils.isNotEmpty(t.getSceneEdificeId())).map(ISceneInfoResp::getSceneEdificeId).collect(Collectors.toSet());
        Set<String> floorSet = respList.stream().filter(t -> StringUtils.isNotEmpty(t.getSceneFloorId())).map(ISceneInfoResp::getSceneFloorId).collect(Collectors.toSet());
        Set<String> roomSet = respList.stream().filter(t -> StringUtils.isNotEmpty(t.getSceneRoomId())).map(ISceneInfoResp::getSceneRoomId).collect(Collectors.toSet());

        Map<String, String> edificeMap = DataRangeHelper.i().approve(() -> getEdificeMap(edificeSet));
        Map<String, String> floorMap = DataRangeHelper.i().approve(() -> getFloorMap(floorSet));
        Map<String, String> roomMap = DataRangeHelper.i().approve(() -> getRoomMap(roomSet));

        for (T t : respList) {
            t.setSceneInfoName(joiner(edificeMap.get(t.getSceneEdificeId()), floorMap.get(t.getSceneFloorId()), roomMap.get(t.getSceneRoomId())));
        }
        return respList;
    }

    /**
     * 获取楼栋id 名称 Map
     *
     * @param ids
     * @return
     */
    public Map<String, String> getEdificeMap(Set<String> ids) {
        if (CollectionUtils.isEmpty(ids)) {
            return new HashMap<>();
        }
        return sceneEdificeService.listByIds(ids)
                .stream()
                .collect(Collectors.toMap(SceneEdificeEntity::getId, SceneEdificeEntity::getEdificeName));
    }

    /**
     * 获取楼层id 名称 Map
     *
     * @param ids
     * @return
     */
    public Map<String, String> getFloorMap(Set<String> ids) {
        if (CollectionUtils.isEmpty(ids)) {
            return new HashMap<>();
        }
        return sceneFloorService.listByIds(ids)
                .stream()
                .collect(Collectors.toMap(SceneFloorEntity::getId, SceneFloorEntity::getFloorName));
    }

    /**
     * 获取空间id 名称 Map
     *
     * @param ids
     * @return
     */
    public Map<String, String> getRoomMap(Set<String> ids) {
        if (CollectionUtils.isEmpty(ids)) {
            return new HashMap<>();
        }
        return sceneRoomService.listByIds(ids)
                .stream()
                .collect(Collectors.toMap(SceneRoomEntity::getId, SceneRoomEntity::getRoomName));
    }

    public String joiner(String... names) {
        return Arrays.stream(names).filter(StringUtils::isNotEmpty).collect(Collectors.joining("/"));
    }

}
